<?
class stats
{
	private $dnsservers = array();
	private $pool_list = array(
			array("pool.sks-keyservers.net", "A"),
			array("pool.sks-keyservers.net", "AAAA"),
			array("pool.sks-keyservers.net", "SRV"),
			array("ipv6.pool.sks-keyservers.net", "AAAA"),
			array("ipv4.pool.sks-keyservers.net", "A"),
			array("eu.pool.sks-keyservers.net", "SRV"),
			array("eu.pool.sks-keyservers.net", "A"),
			array("eu.pool.sks-keyservers.net", "AAAA"),
			array("tls.pool.sks-keyservers.net", "A")
		);
	
	public function get_full_poollist()
	{
		
		return $this->pool_list;
	}

	public function get_pooldata($poolid)
	{
		return $this->pool_list[$poolid];
	}
	
	public function add_dnsserver(&$dnsserver)
	{
		$this->dnsservers[$dnsserver->get_servername()] = $dnsserver; 
	}

	public function get_dnsserver($name)
	{
		return $this->dnsservers[$name];
	}

	public function get_aggregate_date($date)
	{
		$resarr = array();

		foreach($this->get_full_poollist() as $k=>$p)
		{
			foreach($this->dnsservers as $dns)
			{
				$resarr[$k] += $dns->get_date($date, $p[0], $p[1])->get_number();
			}
		}
		
		return $resarr;
	}
}

class dns_stats
{
	private $date;
	private $poolname; 
	private $recordtype;  
	private $number = 0;
	
	public function get_date(){return $this->date;}
	public function get_poolname(){return $this->poolname;}
	public function get_recordtype(){return $this->recordtype;}
	public function get_number(){return $this->number;}

	public function set_date($date)
	{
		$this->date = $date;
	}
	
	public function set_poolname($poolname)
	{
		$this->poolname = $poolname;
	}
	
	public function set_recordtype($type)
	{
		$this->recordtype = $type;
	}
	
	public function set_number($number)
	{
		$this->number = $number;
	}

}

class dns_server
{
	private $servername; 
	private $dns_stats = array(); 

	public function dns_server($name)
	{
		$this->servername = $name; 
	}

	public function get_servername()
	{
		return $this->servername;
	}
	

	public function get_date($date, $poolname, $record)
	{
		foreach($this->dns_stats as $sobj)
		{
			if(
				$sobj->get_poolname() == $poolname 
				&& $sobj->get_recordtype() == $record
				&& $sobj->get_date() == $date
			)
			{
				return $sobj;
			}
		}
		
	}

	public function remove_stats($date, $poolname, $record)
	{
		$removeid = array();
		foreach($this->dns_stats as $skey=>$sobj)
		{
			if(
				$sobj->get_poolname() == $poolname 
				&& $sobj->get_recordtype() == $record
				&& $sobj->get_date() == $date
			)
			{
				$removeid[] = $skey;
			}
		}

		foreach($removeid as $rid)
		{
			unset($this->dns_stats[$rid]);
		}
		
	}

	public function date_exists($date, $poolname, $record)
	{
		if($this->get_date($date, $poolname, $record) === false)
		{
			return false;
		} 
		else
		{
			return true; 
		}
	}

	public function add_stats(&$statsobj)
	{
		$this->dns_stats[] = $statsobj; 	
	}

	public function replace_stats(&$statsobj)
	{
		$this->remove_stats
		(
			$statsobj->get_date(),
			$statsobj->get_poolname(),
			$statsobj->get_recordtype()
		);
		
		$this->add_stats($statsobj);
	}
	
	public function insert_stats(&$statsobj)
	{
		if(
			$this->date_exists($statsobj->get_date(), 			$statsobj->get_poolname(), 
				$statsobj->get_recordtype()
				)
		)
		{
			
			$this->replace_stats($statsobj);
		}
		else
		{
			$this->add_stats($statsobj);
		}
	}

	public function get_datelist()
	{
		$datelist = array();
		foreach($this->dns_stats as $sobj)
		{
			$datelist[$sobj->get_date()] = 1;
		}
		return $datelist; 
	}

	public function get_datelist_from($fromdate)
	{
		$fromdate_time = strtotime($fromdate);
		$datelist = array();
		foreach($this->dns_stats as $sobj)
		{
			if(strtotime($sobj->get_date()) < $fromdate_time) continue; 
			$datelist[$sobj->get_date()] = 1;
		}
		return $datelist; 
	}

	public function get_rawstatsarray()
	{
		return $this->dns_stats; 
	}
}

class generate_dns_stats
{
	private $stats;
	private $number = 0;

	public function generate_dns_stats($servername, $file, $date, $poolname, $recordtype)
	{
		$this->stats = new dns_stats;
		$this->stats->set_date($date);
		$this->stats->set_poolname($poolname);
		$this->stats->set_recordtype($recordtype);
		
		$reparr = array("_pgpkey-http._tcp." => "");

		$fp = file($file);
 		foreach($fp as $l)
 		{
			list($ldate, $ltime, $lc, $lip, $lt2, $lrecord, $lin, $ltype, $lrecursive) = explode(" ", trim($l));
			if(
				date("Y-m-d", strtotime($ldate)) == $date
			)
			{
				if($lrecord == $poolname && $ltype == $recordtype)
				{
					$number++; 
				}
				elseif($recordtype == "SRV" && strtr($lrecord, $reparr) == $poolname)
				{
					$number++;
				}
			}
 		}
		$fp = null; 
		$this->stats->set_number($number);
	}

	public function get_statsobj()
	{
		return $this->stats;
	}
}

?>